package denisLunchy;

import java.sql.Savepoint;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.apache.log4j.*;

public class MVC extends Shell {
	private static View view = null;
	private static Controller control = null;
	private static Model model;
	public Menu menubar;
	public MenuItem mntmSaveDish;
	public MenuItem mntmLoadDish;
	public MenuItem mntmAdd;
	public MenuItem mntmSaveList;
	public MenuItem mntmSaveOrd;
	public MenuItem mntmChange;
	public MenuItem mntmStats;
	TableItem item;
	private static Logger log = Logger.getLogger("simple");

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			PropertyConfigurator.configure("log4j.properties");
			Display display = Display.getDefault();
			MVC shell = new MVC(display);
			setModel(new Model(shell));
			control = new Controller(shell);
			view = new View(shell, SWT.NONE);
			view.setBounds(0, 0, 64, 64);
			view.pack();
			model.getMenu().loadListCl();
			shell.open();
			shell.layout();
			loadDishs();
			loadOrders();
			clientInView();
			view.updateAll();
			log.info("Start programm");
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			log.error("Error", e);
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public MVC(Display display) {
		super(display, SWT.CLOSE | SWT.MIN | SWT.MAX | SWT.TITLE);

		menubar = new Menu(this, SWT.BAR);
		setMenuBar(menubar);

		MenuItem mntmNewSubmenu_3 = new MenuItem(menubar, SWT.CASCADE);
		mntmNewSubmenu_3.setText("Mode");

		Menu menu_3 = new Menu(mntmNewSubmenu_3);
		mntmNewSubmenu_3.setMenu(menu_3);

		mntmChange = new MenuItem(menu_3, SWT.NONE);
		mntmChange.setText("Change");

		mntmStats = new MenuItem(menu_3, SWT.NONE);
		mntmStats.setText("Stats");

		MenuItem mntmNewSubmenu = new MenuItem(menubar, SWT.CASCADE);
		mntmNewSubmenu.setText("Menu");

		Menu menu_1 = new Menu(mntmNewSubmenu);
		mntmNewSubmenu.setMenu(menu_1);

		mntmSaveDish = new MenuItem(menu_1, SWT.NONE);
		mntmSaveDish.setText("Save Menu");

		mntmLoadDish = new MenuItem(menu_1, SWT.NONE);
		mntmLoadDish.setText("Load Menu");

		MenuItem mntmNewSubmenu_1 = new MenuItem(menubar, SWT.CASCADE);
		mntmNewSubmenu_1.setText("Client");

		Menu menu = new Menu(mntmNewSubmenu_1);
		mntmNewSubmenu_1.setMenu(menu);

		mntmAdd = new MenuItem(menu, SWT.NONE);
		mntmAdd.setText("Add");

		mntmSaveList = new MenuItem(menu, SWT.NONE);
		mntmSaveList.setText("Save List");

		MenuItem mntmNewSubmenu_2 = new MenuItem(menubar, SWT.CASCADE);
		mntmNewSubmenu_2.setText("Order");

		Menu menu_2 = new Menu(mntmNewSubmenu_2);
		mntmNewSubmenu_2.setMenu(menu_2);

		mntmSaveOrd = new MenuItem(menu_2, SWT.NONE);
		mntmSaveOrd.setText("Save");
		createContents();

	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Lunchy");
		setSize(470, 480);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public Controller getControl() {
		return control;
	}

	public void setControl(Controller control) {
		this.control = control;
	}

	public static View getView() {
		return view;
	}

	public static void setView(View view) {
		MVC.view = view;
	}

	public static Model getModel() {
		return model;
	}

	public static void setModel(Model model) {
		MVC.model = model;
	}

	public Logger getLogger() {
		return log;
	}

	public static void clientInView() {
		view.clCmb.removeAll();
		if (!model.getMenu().arrayClient.isEmpty()) {
			for (Client client : model.getMenu().arrayClient) {
				view.clCmb.add(client.getSurName() + ','
						+ client.getNameClient());
			}
		}
	}

	public static void loadDishs() {
		ArrayList<Object> load = model.dao.getDishDao().getAll();
		model.getMenu().arrayDish.clear();
		for (int i = 0; i < load.size(); i++) {
			model.getMenu().arrayDish.add((Dish) load.get(i));
		}

		updateTable();
	}

	public void saveDishs() {
		model.dao.getDishDao().clear();
		for (Dish dish : model.getMenu().arrayDish) {
			model.dao.getDishDao().save(dish);
		}
	}

	public static void loadOrders() {
		ArrayList<Object> load = getModel().dao.getOrderDao().getAll();
		getModel().getMenu().arrayOrder.clear();
		for (int i = 0; i < load.size(); i++) {
			getModel().getMenu().arrayOrder.add((Order) load.get(i));
		}
	}

	

	public static void updateTable() {
		Collections.sort(getModel().getMenu().arrayDish, new DishSort());
		view.table.clear(0, view.table.getItemCount() - 1);
		view.table.setItemCount(0);
		int indItem = 0;
		for (Dish dish : getModel().getMenu().arrayDish) {
			if (!dish.getStatus()) {
				view.table.setItemCount(view.table.getItemCount() + 1);
				view.item = view.table.getItem(view.table.getItemCount() - 1);
				view.item.setText(view.dishForTable(dish));
			}
		}
		

	}

	public void hideDish() {
		Dish dish;
		for (int i = 0; i < view.table.getItemCount(); i++) {
			dish = view.dishFromTable(view.table, i);
			for (Dish d : getModel().getMenu().arrayDish) {
				if (d.getName().equals(dish.getName())) {
					if (view.table.getItem(i).getChecked()) {
						d.setStatus(false);
					} else {
						d.setStatus(true);
					}
				}
			}
		}
	}

	public void showDishCl() {
		Client client = view.clientFromView();
		for (Order order : model.getMenu().arrayOrder) {
			if (order.getClient().getNameClient()
					.equals(client.getNameClient())) {
				if (order.getClient().getSurName().equals(client.getSurName())) {
					for (int i = 0; i < view.table.getItemCount(); i++) {
						item = view.table.getItem(i);
						for (Dish dish : order.arrayDish) {
							if (dish.getName().equals(item.getText(1))) {
								item.setChecked(true);
								break;
							} else {
								item.setChecked(false);
							}
						}
					}
				}
			}
		}
	}

	public void showStats() {

	}

	public void statsCl() {
		int i=0;
		int itmCnt = 0;
		TableItem item;
		String[] stats = new String[3];
		for(Dish dish:getModel().getMenu().arrayDish){
			if(!dish.getStatus()){
				itmCnt++;
			}
		}
		View.tableStats.setItemCount(itmCnt);
		for(Dish dish:getModel().getMenu().arrayDish){
		if(!dish.getStatus()){
		item=View.tableStats.getItem(i);
		stats[0]=dish.getName();
		stats[1]=Integer.toString(dish.calcCount(getModel().getMenu()));
		stats[2]=Integer.toString(Integer.parseInt(stats[1])*dish.getCost());
		item.setText(stats);
	
		i++;
		}
		
		}


	}

	public void updateChecked() {
		String name;
		String srName;
		Order order;
		TableItem items;
		Dish dish;
		for (int i = 0; i < view.table.getItemCount(); i++) {
			items = view.table.getItem(i);
			if (items.getChecked()) {
				name = view.clCmb.getItem(view.clCmb.getSelectionIndex())
						.split(",")[1];
				srName = view.clCmb.getItem(view.clCmb.getSelectionIndex())
						.split(",")[0];
				order = getModel().getMenu().findOrder(name, srName);
				dish=new Dish();
				dish=getModel().getMenu().findDish(items.getText(1), order.arrayDish);
				items.setText(view.dishForTable(dish));
			}

		}
	}

}
